<?php

namespace App\Http\Controllers\Home;

use App\Model\Group_member;
use App\Model\Leave;
use App\Model\Role;
use App\Model\Sch_desc;
use App\Model\Scheduling;
use App\Model\Staff;
use App\Model\Staff_task;
use App\Model\Station;
use App\Model\Store;
use App\Model\Users;
use App\Model\Workflow;
use App\Model\Workflow_desc;
use App\Model\Workflow_group;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use League\Flysystem\Exception;
use Ramsey\Uuid\Uuid;
//班次安排
class SchedulingController extends CommonController
{


	protected  $model;
	protected $store;
	protected $station;
	protected $staff;
	protected $scheduling;
	protected $sch_desc;
	protected $Staff_task;
	protected $Workflow;
	protected $Workflow_desc;
	protected static $uid;
	protected $Users;
	protected $Role;
	protected $Leave;
	protected static $userInfo;
	protected $Group_member;
	protected $Workflow_group;
	public function __construct()
	{
		parent::__construct();
		$this->Users=new Users();
		$this->store=new Store();
		$this->Leave=new Leave();
		$this->Role=new Role();
		$this->station=new Station();
		$this->staff=new Staff();
		$this->scheduling=new Scheduling();
		$this->sch_desc=new Sch_desc();
		$this->Staff_task=new Staff_task();
		$this->Workflow=new Workflow();
		$this->Workflow_desc=new Workflow_desc();
		$this->Group_member=new Group_member();
		$this->Workflow_group=new Workflow_group();
		self::$uid=Auth::user()->id;
		self::$userInfo=Auth::user();


	}

//	排班列表
	public function sch_list(Request $request){
		try{
			$where['company_id']=$this->company_id;
			$where['store_id']=$this->store_id;
			$where['class_name']=isset($request->class_name)?$request->class_name:null;
			$where['start_time']=isset($request->start_time)?$request->start_time:null;
			$where['end_time']=isset($request->end_time)?$request->end_time:null;
			$where['status']=isset($request->status)?$request->status:null;
			$data= $this->scheduling->lists($where);
		}catch (Exception $exception){
			$message=$exception->getMessage();
		}

		if(!isset($message)){
			return $this->success($data);
		}else{
			return $this->failed($message);
		}
	}


//	获取排班人员列表

	public function scheduling_staff(Request $request){
//		$time=$request->time;
		$data=$this->Staff_task->staff_list($this->store_id);
		$newArr=[];$Arr=[];
		if(count($data)){
			foreach($data as $kk=>$vv){
				if($vv['staffInfo'] !=null){
					$Arr[$kk]['staff_id']=$vv['staff_id'];
					$Arr[$kk]['staff_task_id']=$vv['task_id'];
					$Arr[$kk]['staff_name']=$vv['staff_info']['name'];
					$Arr[$kk]['station_id']=$vv['store_station']['id'];
					$Arr[$kk]['station_name']=$vv['store_station']['name'];
				}

			}
			$newArr=array_merge($newArr,$Arr);
		}

//		获取该店铺所有的正式员工
		$formal_data=$this->staff->allFormal($this->store_id);
//		if(count($formal_data)){
//			foreach($formal_data as $k=>$v){
//				$Arr_formal[$k]['staff_id']=$v['id'];
//				$Arr_formal[$k]['staff_name']=$v['name'];
//				$Arr_formal[$k]['staff_task_id']=null;
//				$Arr_formal[$k]['station_id']=$v['store_station']['id'];
//				$Arr_formal[$k]['station_name']=$v['store_station']['name'];
//			}
//			$newArr=array_merge($newArr,$Arr_formal);
//		}
		return $this->success($newArr);
	}



//	新增排班
	public function create(Request $request){
//		模拟数据
		$class_name=date("Ymd",time()).rand(1,999);
//		$data=[
////			'time'=>$this->month_allday(),
//			'time'=>['2018-08-01','2018-08-02'],
//			'status'=>1,
//			'type'=>'月',
//			'desc'=>[
//				[
//					'station_id'=>1,
//					'staff_id'=>'91',
//					'staff_name'=>'员工姓名',
//					'staff_task_id'=>19,
//					'sch'=>[['start_time'=>'08:00','end_time'=>'18:30']],
//					'leavel'=>[['start_time'=>'08:00','end_time'=>'08:30']],
//
//				],
//				[
//					'station_id'=>1,
//					'staff_id'=>'89',
//					'staff_name'=>'员工姓名',
//					'staff_task_id'=>17,
//					'sch'=>[['start_time'=>'08:00','end_time'=>'18:30']],
//					'leavel'=>[['start_time'=>'08:00','end_time'=>'08:30']],
//
//				],
//				[
//					'station_id'=>3,
//					'staff_id'=>'90',
//					'staff_name'=>'员工姓名',
//					'staff_task_id'=>18,
//					'sch'=>[['start_time'=>'08:00','end_time'=>'18:30']],
//					'leavel'=>[['start_time'=>'08:00','end_time'=>'08:30']],
//				]
//			],
//
//		];
//
//		return $data;
		$data=$request->input();
//		新增排班
		$data_sch=[
			'type'=>$data['type'],
			'class_name'=>$class_name,
			'status'=>$data['status'],
			'company_id'=>$this->company_id,
			'store_id'=>$this->store_id,
			'start_day'=>min($data['time']),
			'end_day'=>max($data['time']),
		];
		DB::beginTransaction();
		try{
			$id=$this->scheduling->create_sch($data_sch);
			$res=$this->sch_desc->create_sch_desc($id,$data,$this->store_id,$this->company_id);
			$this->update_task($data['desc']);
			$this->staff->update_staff($data);
			$this->binding($id);
			$this->create_leave($data['desc'],$data['time'],$id);
//			DB::commit();
		}catch (\Exception $exception){
			DB::rollback();
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->stored(['code'=>201,'messaage'=>'添加成功']);
		}else{
			return $this->failed($message);
		}
	}

//	编辑排班
//	public function edit(Request $request){
//		//		模拟数据
//		$id=$request->id;
//		$status=$request->status;
//		$data=[
//			'time'=>'2018-04-23,2018-04-22',
//			'desc'=>[
//				[
//					'station_id'=>1,
//					'staff_id'=>'1',
//					'start_time'=>' 08:00',
//					'end_time'=>'18:00',
//				],
//				[
//					'station_id'=>2,
//					'staff_id'=>'2',
//					'start_time'=>'11:00',
//					'end_time'=>'17:00',
//				]
//			],
//
//		];
//
////		新增排班
//		$data_sch=[
//			'status'=>$status,
//		];
//		$data['time']=explode(',',$data['time']);
//		$data_sch_desc=$data['desc'];
//		DB::beginTransaction();
//		try{
//			$id=$this->scheduling->where(['id'=>$id])->update($data_sch);
//			$this->sch_desc->where(['scheduling_id'=>$id])->delete();
//			$res=$this->sch_desc->create_sch_desc($id,$data_sch_desc,$data['time']);
//			DB::commit();
//
//		}catch (\Exception $exception){
//			DB::rollback();
//			$message=$exception->getMessage();
//		}
//		if(!isset($message)){
//			return $this->updated('更新成功');
//		}else{
//			return $this->failed($message);
//		}
//	}




//	编辑排班
	public function edit(Request $request){
		$scheduling_id=$request->scheduling_id;
		$schedulingData=$this->scheduling->where(['id'=>$scheduling_id])->first();
		$time=isset($request->time)?$request->time:$schedulingData['start_day'];
		$data=$this->sch_desc->desc($scheduling_id,$time);
		$leavelData=$this->Leave->all_leavel($this->store_id,$time);
		$newArr=[];$str=[];$array=[];
		foreach($data as $k=>$v){
			$newArr[$k]['staff_id']=$v['staff_id'];
			$newArr[$k]['staff_name']=$v['staff_info']['name'];
			$newArr[$k]['station_id']=$v['station_id'];
			$newArr[$k]['station_name']=$v['station']['name'];
			$newArr[$k]['staff_task_id']=$v['staff_task_id'];
			$newArr[$k]['start_time']=$v['start_time'];
			$newArr[$k]['end_time']=$v['end_time'];
//			$newArr[$k]['leavel']=isset($leavelData[$v['staff_id']])?$leavelData[$v['staff_id']]:null;
		}
		$array=[];
		foreach($newArr as $key=>$value){
			$array[$value['staff_id']]['staff_id']=$value['staff_id'];
			$array[$value['staff_id']]['staff_name']=$value['staff_name'];
			$array[$value['staff_id']]['station_id']=$value['station_id'];
			$array[$value['staff_id']]['station_name']=$value['station_name'];
			$array[$value['staff_id']]['staff_task_id']=$value['staff_task_id'];
			$array[$value['staff_id']]['sch'][$key]['start_time']=$value['start_time'];
			$array[$value['staff_id']]['sch'][$key]['end_time']=$value['end_time'];
			$array[$value['staff_id']]['leave']=$this->level($time,$value['staff_id'],$value['staff_task_id']);
		}

		foreach($array as $kk=>$vv){
			$array[$kk]['sch']=array_values($vv['sch']);
		}
		$array=array_values($array);

		return $this->success($array);

	}

// 编辑排班获取日期和人员
	public function staff_date(Request $request){
		$scheduling_id=$request->scheduling_id;
		$data['date']=$this->sch_desc->where(['scheduling_id'=>$scheduling_id])->distinct('time')->pluck('time');
		$data['satff']=$this->sch_desc->staff_date($scheduling_id);
		return $this->success($data);
	}

//	编辑排班保存
	public function edit_save(Request $request){
//		删除这一天排班数据和请假数据
		$scheduling_id=$request->scheduling_id;
//		$time=$request->time;
//		模拟数据
//		$data=[
//			'time'=>['2018-06-01'],
//			'scheduling_id'=>1,
//			'desc'=>[
//				[
//					'station_id'=>1,
//					'staff_id'=>'91',
//					'staff_name'=>'员工姓名',
//					'staff_task_id'=>19,
//					'sch'=>[['start_time'=>'08:00','end_time'=>'18:30']],
//					'leavel'=>[['start_time'=>'08:00','end_time'=>'08:30']],
//
//				],
//				[
//					'station_id'=>1,
//					'staff_id'=>'89',
//					'staff_name'=>'员工姓名',
//					'staff_task_id'=>17,
//					'sch'=>[['start_time'=>'08:00','end_time'=>'18:30']],
//					'leavel'=>[['start_time'=>'08:00','end_time'=>'08:30']],
//
//				],
//				[
//					'station_id'=>3,
//					'staff_id'=>'90',
//					'staff_name'=>'员工姓名',
//					'staff_task_id'=>18,
//					'sch'=>[['start_time'=>'08:00','end_time'=>'18:30']],
//					'leavel'=>[['start_time'=>'08:00','end_time'=>'08:30']],
//				]
//			],
//
//		];
//		模拟数据结束
		$data=$request->input();
		DB::beginTransaction();
		try{
			$this->sch_desc->where(['scheduling_id'=>$scheduling_id,'time'=>$data['time']])->delete();
			$this->Leave->where(['scheduling_id'=>$scheduling_id,'time'=>$data['time']])->delete();
			$res=$this->sch_desc->create_sch_desc($data['scheduling_id'],$data,$this->store_id,$this->company_id);
			$this->create_leave($data['desc'],$data['time'],$data['scheduling_id']);
			DB::commit();
		}catch (Exception $exception){
			$message=$exception->getMessage();
			DB::rollBack();
		}
//		添加这一天的排班数据请假数据
		if(!isset($message)){
			return $this->updated(['code'=>200,'message'=>'更新成功']);
		}else{
			return $this->failed($message);
		}
	}
//	创建工单
	public function create_work_order($data){

		$days=count($data['time']);
		foreach($data['desc'] as $kk=>$vv){
			$station=$this->station($vv['station_id']);
			$staff_info=$this->staff_info($vv['staff_id']);
			$datetime1 = strtotime('2012-11-23  '.$vv['start_time']);
			$datetime2 = strtotime('2012-11-23  '.$vv['end_time']);
			$hour=number_format(($datetime2-$datetime1)/3600,1);
			$newArr[$kk]['staff_id']=$vv['staff_id'];
			$newArr[$kk]['store_id']=$this->store_id;
			$newArr[$kk]['staff_name']=$staff_info->name;
			$newArr[$kk]['store_station_id']=$vv['station_id'];
			$newArr[$kk]['station_name']=$station->name;
			$newArr[$kk]['should_hour']=$hour*$days;
			$newArr[$kk]['hour_salary']=$station->hour_salary;
			$newArr[$kk]['standard_money']=$newArr[$kk]['should_hour']*$newArr[$kk]['hour_salary'];
		}
		return $newArr;
	}

//	查询店铺岗位的详细信息
	public function station($station_id){
		return $this->station->station_desc($station_id);
	}

//	查询员工的详细信息
	public function staff_info($staff_id){
		return $this->staff->staff_desc($staff_id);
	}

//	修改任务状态
	public function update_task($data){
		foreach($data as $kk=>$vv){
			$array[]=$vv['staff_task_id'];
		}
		$this->Staff_task->whereIn('id',$array)->update(['status'=>4]);
	}

//	绑定审核流程
	public function binding($id){
		 $workflowData=$this->Workflow->where(['company_id'=>$this->company_id,'style'=>1,'status'=>1])
			->first();
		$group_id=$this->belongsToGroup(self::$userInfo->department);
//		查询该用户的审批流
		$workflow_id=$this->Workflow_group->getworkflow_id($group_id,1);
		$workflow_desc['lovel_uid']=$this->Role->Role_user($workflowData->one);
		$workflow_desc['code']=Uuid::uuid4();
		foreach($workflow_desc['lovel_uid'] as $kk=>$vv){
			$data[$kk]['workflow_id']=$workflow_id['id'];
			$data[$kk]['from_uid']=self::$uid;
			$data[$kk]['lovel_role']=$workflowData->one;
			$data[$kk]['lovel_uid']=$vv;
			$data[$kk]['settlement_id']=$id;
			$data[$kk]['code']=$workflow_desc['code'];
			$data[$kk]['type']=1;
			$data[$kk]['created_at']=date("Y-m-d H:i:s",time());
		}
		DB::table('workflow_desc')->insert($data);
	}
//	添加中止
	public function create_leave($leavelData,$time,$id){
		foreach($time as $k=>$v){
			foreach($leavelData as $kk=>$vv){
				foreach($vv['leavel'] as $key=>$value){
					$newArr=[];
					$newArr['store_id']=$this->store_id;
					$newArr['staff_id']=$vv['staff_id'];
					$newArr['store_station_id']=$vv['station_id'];
					$newArr['name']=$vv['staff_name'];
					$newArr['staff_task_id']=$vv['staff_task_id'];
					$newArr['scheduling_id']=$id;
					$newArr['type']=4;
					$newArr['date_time']=$v;
					$newArr['start_time']=$v.' '.$value['start_time'];
					$newArr['end_time']=$v.' '.$value['end_time'];
					$newArr['time']=(strtotime($newArr['end_time'])-strtotime($newArr['start_time']))/3600;
					$this->Leave->updateOrCreate(['staff_id'=>$newArr['staff_id'],'start_time'=>$newArr['start_time'],
						'end_time'=>$newArr['end_time']],$newArr);
				}
			}
		}
	}

//	获取本月所有的天数
	public function month_allday(){
		$j = date("t"); //获取当前月份天数
		$start_time = strtotime(date('Y-m-01'));  //获取本月第一天时间戳
		$array = array();
		for($i=0;$i<$j;$i++){
			$array[] = date('Y-m-d',$start_time+$i*86400); //每隔一天赋值给数组
		}
			return $array;
	}

//	获取员工在某一天的中止时间
	public function level($time,$staff_id,$staff_task_id){
		$data=$this->Leave->where([
			'date_time'=>$time,
			'staff_id'=>$staff_id,
			'staff_task_id'=>$staff_task_id,
		])->get();
		$newArr=[];
		foreach($data as $key=>$value){
			$newArr[$key]['start_time']=date("Y-m-d",strtotime($value['start_time']));
			$newArr[$key]['end_time']=date("Y-m-d",strtotime($value['end_time']));
		}
		return $newArr;
	}

//	查询用户归属用户组
	public function belongsToGroup($department_id){
		$data=$this->Group_member->where(['department_id'=>$department_id])->first();
		$group_id=$data->group_id;
		return $group_id;
	}


//	查询
	public function getworkflow_id($group_id){
		return $this->Workflow_group->where(['group_id'=>$group_id])->get();
	}
}
